//用户提醒
let told = document.getElementById('told');
let toTold = document.getElementById('toTold');
document.getElementById('toldClose').addEventListener('click', function() {
    told.style.display = 'none';
})

function close() {
    told.style.display = 'none';
};

function open() {
    told.style.display = 'block';
};
document.getElementById('toldClose').addEventListener('click', function() {
    told.style.display = 'none';
})

//  ***************************************************七天免登录************************************************************
let username = document.getElementById("username");
let password = document.getElementById("password");
let unlog = document.getElementById("unlog");
// 页面初始化时，如果账号密码cookie存在则将其填上
if (getCookie('username') != undefined && getCookie('username').length > 0) {
    // document.getElementById("username").value = getCookie('username');
    username.value = getCookie('username');
    unlog.checked = true;
    // alert("gogo");
}
if (getCookie('password') != undefined && getCookie('password').length > 0) {
    // document.getElementById("password").value = getCookie('password');
    password.value = getCookie('password');
    unlog.checked = true;
    // alert("toto");
}

// *********************************************************cookie操作************************************************
//复选框勾选状态改变，如果未勾选则清除cookie
unlog.onchange = function() {
    if (!this.checked) {
        delCookie('username');
        delCookie('password');
    }
};
//表单提交事件被触发时，如果复选框是勾选状态则保存cookie
document.getElementById("login").addEventListener('click', function() {
        if (unlog.checked) {
            setCookie("username", username.value, 7); //保存账号到cookie,有效期7天
            setCookie("password", password.value, 7); //保存密码到cookie,有效期7天
        }
    })
    //设置cookie
function setCookie(key, value, day) {
    let date = new Date();
    date.setDate(date.getDate() + day);
    // expirse设置过期时间
    //;后面要加一个空格，因为多个cookie之间有个空格
    //date.toGMTString() 将时间改为格林威治时间字符串
    document.cookie = key + '=' + value + '; path=/; expirse=' + date.toGMTString();
};
// //获取cookie
// function getCookie(username) {
//     var reg = eval("/(?:^|;\\s*)" + username + "=([^=]+)(?:;|$)/");
//     alert(reg);
//     return reg.test(document.cookie) ? RegExp.$1 : "";
// };
//获取cookie
// *****************************************************************************getcookie有问题****************************************************
function getCookie(username) {
    // var reg = RegExp(username + '=/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/');
    // var arr = document.cookie.match(reg);
    // if (arr) {
    //     return arr[1];
    // } else {
    //     return '';
    // }
    var cookie = document.cookie;
    var arr = cookie.split(";");
    for (let i = 0; i < arr.length; i++) {
        var newArr = arr[i].split("=");
        if (newArr[0] == username) {
            return newArr[1];
        }
    }

};

//删除cookie
function delCookie(key) {
    setCookie(key, null, -1);
};
//   ***************************************************************************************************************************
//   ********************************************记住账号密码********************************************************************

let v_uname = localStorage.getItem('username');
let v_pwd = localStorage.getItem('password');
if (v_uname !== undefined && v_uname !== '') {
    username.value = localStorage.getItem('username');
}
if (v_pwd != undefined && v_pwd != '') {
    password.value = localStorage.getItem('password');
}
// ******************************************************************************************************************************
document.getElementById("login").onclick = function() {
        let flag = 1;
        let lo_data = new FormData();
        lo_data.append("username", document.getElementById("username").value);
        lo_data.append("password", document.getElementById("password").value);
        var remember = document.getElementById('remember');
        var error_name = document.getElementById("error_name");
        var error_pwd = document.getElementById("error_pwd");

        //账户名校验
        function verifyName() {
            var verifyName = document.getElementById("username").value;
            var test_name = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/; //含字母和数字8-16位
            if (!test_name.test(verifyName)) {
                error_name.innerHTML = "警告：用户名格式错误！！";
                flag = 0;
                return false;
            } else {
                flag = 1;
                return true;
            }
        };
        verifyName();
        //密码校验
        function verifyPwd() {
            var verifyPwd = document.getElementById("password").value;
            var test_pwd = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/; //含字母和数字8-16位
            if (!test_pwd.test(verifyPwd)) {
                error_pwd.innerHTML = "警告：密码格式错误！！";
                flag = 0;
                return false;
            } else {
                flag = 1;
                return true;
            }
        };
        verifyPwd();

        // 记住密码
        function savepwd() {
            if (remember.checked) {
                var username = document.getElementById("username").value;
                var password = document.getElementById("password").value;
                window.localStorage.username = username;
                window.localStorage.password = password;
                localStorage.rmbPassword = true;
            } else {
                localStorage.rmbPassword = false;
            }
        };
        savepwd();

        // 创建xhr对象
        let xhr = new XMLHttpRequest();

        // 设置请求url
        let url = "http://www.rushmc.top/api/login";
        if (flag == 1) {
            // 打开链接
            xhr.open("post", url, true);
            // 发送请求
            xhr.send(lo_data);
            // 设置xhr对象readystate变化时响应函数
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    let res = JSON.parse(xhr.responseText);
                    if (res.code === 200) {
                        window.sessionStorage.visitor = 0;
                        window.localStorage.id = res.data.id;
                        window.location.href = 'index.html';
                        //用户个人信息保存
                        function saveUser() {
                            localStorage.id = res.data.id;
                            localStorage.myname = res.data.name;
                            localStorage.password = res.data.password;
                            localStorage.phone = res.data.phone;
                            localStorage.username = res.data.username;
                        }
                        saveUser();
                    } else {
                        open();
                        setTimeout('close()', 3000);
                        console.log(res.msg);
                    }
                }
            }
        }
    }
    //****************************************************************************************************************
    // 修改密码模块
document.getElementById("confirm").onclick = function() {
    let flag = 1;
    let newdata = new FormData();
    newdata.append("id", localStorage.getItem('id'));
    newdata.append("password", document.getElementById("newpassword").value);
    var c_error_name = document.getElementById("c_error_name");
    var c_error_pwd = document.getElementById("c_error_pwd");
    var error_newpwd = document.getElementById("error_newpwd");
    var error_rep_pwd = document.getElementById("error_rep_pwd");

    //账户名校验
    function verifyName() {

        var verifyName = document.getElementById("c_username").value;
        var test_name = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/; //含字母和数字8-16位
        if (!test_name.test(verifyName)) {
            c_error_name.innerHTML = "警告：用户名格式输入错误！！";
            flag = 0;
            return false;
        } else {
            flag = 1;
            return true;
        }
    };
    verifyName();
    //原密码校验
    function verifyPwd() {

        var verifyPwd = document.getElementById("c_password").value;
        var test_pwd = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/; //含字母和数字8-16位
        if (!test_pwd.test(verifyPwd)) {
            c_error_pwd.innerHTML = "警告：密码格式输入错误！！";
            flag = 0;
            return false;
        } else {
            flag = 1;
            return true;
        }
    };
    verifyPwd();

    //新密码校验
    function verifynewPwd() {

        var verifyPwd = document.getElementById("newpassword").value;
        var test_pwd = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/; //含字母和数字8-16位
        if (!test_pwd.test(verifyPwd)) {
            error_newpwd.innerHTML = "警告：密码格式输入错误！！";
            flag = 0;
            return false;
        } else {
            flag = 1;
            return true;
        }
    };
    verifynewPwd();
    //确认密码校验
    function verifyCheckPwd() {
        var verifyOldPwd = document.getElementById("c_password").value;
        var verifyNewPwd = document.getElementById("newpassword").value;
        if (verifyOldPwd != verifyNewPwd) {
            error_rep_pwd.innerHTML = "警告：两次密码不一致！！";
            flag = 0;
            return false;
        } else {
            flag = 1;
            return true;
        }
    };
    verifyCheckPwd();

    let changexhr = new XMLHttpRequest();
    let changeurl = "http://www.rushmc.top/api/passwordUpdate";
    if (flag == 1) {
        changexhr.open("post", changeurl, true);
        changexhr.send(newdata);
        changexhr.onreadystatechange = function() {
            if (changexhr.readyState === 4 && changexhr.status === 200) {
                let c_res = JSON.parse(changexhr.responseText);
                if (c_res.code === 200) {
                    console.log(c_res.msg);
                    window.location.href = 'index.html';
                } else {
                    console.log(c_res.msg);
                }
            }
        }
    }
}